<?php


namespace app\index\controller\xilujob;


use app\common\controller\xilujob\Xilujob;
use app\common\model\xilujob\PersonalMessage;
use app\common\model\xilujob\ResumePersonalInfo;
use app\common\model\xilujob\XilujobUser;
use app\index\model\xilujob\SystemMessage;

class Message extends Xilujob
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $noNeedEnterpriseAudit = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }
    //消息通知
    public function index() {

        if($this->request->isAjax()){
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = PersonalMessage::where('company_id', $this->company->id)
                ->where('user_group_id',2)
                ->where($where)
                ->order('id','desc')
                ->paginate($limit)->each(function ($row){
                    switch ($row['type']) {
                        case PersonalMessage::TYPE_APPLY_SENT:
                            #用户投递简历-给企业发消息
                            $row->url = url('xilujob.job_apply/index');
                            break;
                        case PersonalMessage::TYPE_INTERVIEW_SENT:
                            $row->url = url('xilujob.user/interview_invitation');
                            break;
                        case PersonalMessage::TYPE_INTERVIEW_REJECTED:
                            #用户拒绝面试-给企业发消息
                            $row->url = url('xilujob.job_interview/index').'?status=1';
                            break;
                        case PersonalMessage::TYPE_INTERVIEW_ACCEPTED:
                            #用户同意面试-给企业发消息
                            $row->url = url('xilujob.job_interview/index').'?status=2';
                            break;
                    }
                    if($row->extra && isset($row->extra['resume_id'])){
                        $row->personal = ResumePersonalInfo::where('resume_id',$row->extra['resume_id'])->find();
                    }else{
                        $row->personal = [];
                    }

                });

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
//        $xilujobuser = XilujobUser::get(['user_id'=>$this->auth->id]);
//        if($xilujobuser->personal_message>0) {
//            $xilujobuser->save(['personal_message'=>0]);
//        }
        $this->view->assign('title', '消息中心');
        return $this->view->fetch();
    }

    //系统消息
    public function system() {

        if($this->request->isAjax()){
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = SystemMessage::where('type', SystemMessage::TYPE_SYSTEM)
                ->order('id','desc')
                ->paginate();

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
//        $xilujobuser = XilujobUser::get(['user_id'=>$this->auth->id]);
//        if($xilujobuser->system_message>0) {
//            $xilujobuser->save(['system_message'=>0]);
//        }
        $this->view->assign('title', '消息中心');
        return $this->view->fetch();
    }
}